package com.k2fsa.sherpa.onnx;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Objects;

public class MainPresenter {

    private Activity context;
    public HashMap<String, OnActionOperation> actions = new HashMap<>();

    public String[] keys = {
            "打开设置",
            "打开相册",
            "打开文件预览",
            "打开图片预览",
            "关闭文件预览",
            "关闭图片预览",
            "看图片",
    };

    public OnActionOperation[] operations = {
            action -> {
                //启动系统设置
                Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
                context.startActivity(intent);
            },
            action -> {
                //启动系统相册
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                context.startActivityForResult(intent, 1);
            },
            action -> {
                //打开文件预览
                Intent intent = new Intent();
                intent.setClassName("com.filepreview.application",
                        "com.filepreview.application.activity.DocumentListActivity");
                context.startActivityForResult(intent, 1);
            },
            action -> {
                //打开图片预览
                Intent intent = new Intent();
                intent.setClassName("com.filepreview.application",
                        "com.filepreview.application.activity.ImageListActivity");
                context.startActivityForResult(intent, 2);
            },
            action -> {
                //关闭文件预览
                context.finishActivity(1);
            },
            action -> {
                context.finishActivity(2);
            },
            action -> {
                //打开图片预览
                Intent intent = new Intent();
                intent.setClassName("com.filepreview.application",
                        "com.filepreview.application.activity.ImageListActivity");
                context.startActivityForResult(intent, 2);
            },
    };

    public void init(Activity context) {
        this.context = context;
        for (int i = 0; i < keys.length; i++) {
            actions.put(keys[i], operations[i]);
        }
    }

    public void operation(@NotNull String lastText, Handler handler) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < keys.length; i++) {
                    if (lastText.contains(keys[i])) {
                        Objects.requireNonNull(actions.get(keys[i])).operation(keys[i]);
                        handler.sendEmptyMessage(100);
                    }
                }
            }
        });
    }

    public interface OnActionOperation {
        void operation(String action);
    }

}
